package com.soap.desensitize.posterity.collection;

import com.soap.desensitize.encry.IDesensitization;
import com.soap.desensitize.exception.strategy.DecodeException;
import com.soap.desensitize.exception.strategy.EncodeException;
import com.soap.desensitize.posterity.AbstractPosterity;
import com.soap.desensitize.strategy.rreversible.symmetry.DesStrategy;

import java.util.Map;

/**
 * @desc List加密
 */
public class MapDesensitization extends AbstractPosterity<Map> {
    /**
     * @desc 无参构造器，默认使用DES加密
     */
    private MapDesensitization() {
        super(DesStrategy.getInstance());
    }

    private static MapDesensitization instance = null;
    /**
     * @desc 获得实例
     * @return
     */
    public static synchronized MapDesensitization getInstance(IDesensitization<Object> encry){
        if(instance == null){
            instance = new MapDesensitization();
        }
        return instance;
    }
    /**
     * @param encry
     * @desc 有参构造器
     */
    public MapDesensitization(IDesensitization encry) {
        super(encry);
    }

    @Override
    public Map encode(Map data) throws EncodeException {
        for (Object key: data.keySet()) {
            Object value = data.get(key);
            Object encodeValue = judgeAndEncode(value);
            data.put(key,encodeValue);
        }
        return data;
    }

    @Override
    public Map decode(Map data) throws DecodeException {
        for (Object key: data.keySet()) {
            Object value = data.get(key);
            Object decodeValue = judgeAndDecode(value);
            data.put(key,decodeValue);
        }
        return data;
    }
}
